package com.feicheng.controller.system;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.entity.Role;
import com.feicheng.persistence.mapper.ClientMapper;
import com.feicheng.persistence.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/admin/client")
public class ClientController {

    @Autowired
    IClientService iClientService;

    @Autowired
    ClientMapper clientMapper;

    @GetMapping("/page")
    public ResultInfo getClientsPage(
            Client client,
            @RequestParam(defaultValue = "1")Integer current,
            @RequestParam(defaultValue = "10")Integer size){

            IPage<Client> page = new Page<>(current, size);

            QueryWrapper<Client> qw = new QueryWrapper<>();

            return ResultInfo.success(iClientService.page(page, qw));
    }

    @GetMapping("/info")
    public ResultInfo getDetail(String clientid) {
        return ResultInfo.success(iClientService.getById(clientid));

    }

    @GetMapping("/yj/page")
    public ResultInfo getClientsYjPage(
            @RequestParam(defaultValue = "1")Integer current,
            @RequestParam(defaultValue = "10")Integer size){
        Page<JSONObject> page = new Page<>(current, size);
        QueryWrapper<JSONObject> qw = new QueryWrapper<>();

        return ResultInfo.success(clientMapper.getClientYjPage(page, qw));
    }

    @GetMapping("/yj/info")
    public ResultInfo getClientsYjInfo(String clientid) {
        JSONObject result = new JSONObject();

        result.put("client", iClientService.getById(clientid));

        QueryWrapper<Client> qw = new QueryWrapper<>();
        qw.eq("fromid", clientid);

        result.put("tjClients", iClientService.list(qw));

        return ResultInfo.success(result);
    }
}

